import { Inject, Post, Query } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { CommunityPostEntity } from '../../entity/post';
import { CommunityPostService } from '../../service/post';
import { CommunityTagEntity } from '../../entity/tag';
import { CommunityPostTagEntity } from '../../entity/post_tag';
import { UserInfoEntity } from '../../../user/entity/info';

/**
 * 社区帖子
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: CommunityPostEntity,
  service: CommunityPostService,
  pageQueryOp: {
    keyWordLikeFields: ['a.content', 'a.userNickname'],
    fieldEq: ['a.status', 'a.topStatus'],
    where: async ctx => {
      const { tagIds, startTime, endTime } = ctx.request.body;
      const where: any = [];
      if (tagIds && tagIds.length > 0) {
        where.push(
          `exists (select 1 from community_post_tag cpt where cpt.postId = a.id and cpt.tagId in (${tagIds.join(
            ',',
          )}))`,
        );
      }
      if (startTime) {
        where.push(`a.createTime >= '${startTime}'`);
      }
      if (endTime) {
        where.push(`a.createTime <= '${endTime}'`);
      }
      return where;
    },
  },
})
export class AdminCommunityPostController extends BaseController {
  @Inject()
  communityPostService: CommunityPostService;
}
